import java.util.Stack;

public class NextGreaterElements {

    public int[] nextGreaterElements(int[] nums) {
        int[] temp = new int[nums.length];
        for (int i = 0; i < temp.length; i++) {
            temp[i] = -1;
        }
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < nums.length * 2; i++) {
            int k = i % nums.length;
            while (!stack.isEmpty() && nums[stack.peek()] < nums[k]) {
                Integer j = stack.pop();
                temp[j] = nums[k];
            }
            stack.push(k);
        }
        return temp;
    }
}
